package app.controllers.mobileweb;

import app.Const;
import app.dtos.ActivityDTO;
import app.interceptors.MobileWxMemberInterceptor;
import app.models.activity.BrandActivityLike;
import app.models.activity.BrandActivityShare;
import app.models.brand.BrandAttention;
import com.github.sog.controller.BasicController;
import com.github.sog.libs.AppFunc;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.aop.Before;
import com.jfinal.log.Logger;

import java.util.List;

/**
 * <p>
 * .收藏
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-04-22 10:53
 * @since JDK 1.6
 */
@Before(MobileWxMemberInterceptor.class)
public class CollectionController extends BasicController {
    public static final String SHARE = "share";
    public static final String LIKE  = "like";
    Logger logger = Logger.getLogger(this.getClass());

    /**
     * 收藏默认页面
     */
    public void index() {
        int mid = getParaToInt(Const.MEMBER_PARAM, 0);
        List<BrandAttention> attentionList = BrandAttention.dao.findByMemberId(mid);
        setAttr("attentionList", attentionList);
        //默认我的关注
        render("mf_attention.ftl");
    }

    /**
     * 我的关注
     */
    public void attention() {
        int mid = getParaToInt(Const.MEMBER_PARAM, 0);
        logger.debug("attention---------------mid=" + mid);

        List<BrandAttention> attentionList = BrandAttention.dao.findByMemberId(mid);

        setAttr("attentionList", attentionList);
        render("mf_attention.ftl");
    }

    /**
     * 我的分享
     */
    public void share() {
        int mid = getParaToInt(Const.MEMBER_PARAM, 0);
        logger.debug("share---------------mid=" + mid);
        setAttr(Const.MEMBER_PARAM, mid);

        List<ActivityDTO<BrandActivityShare>> dtoList = Lists.newArrayList();
        ActivityDTO<BrandActivityShare> dto;
        List<BrandActivityShare> balList = BrandActivityShare.dao.findByMemberId(mid);

        for (BrandActivityShare bal : balList) {
            dto = new ActivityDTO<BrandActivityShare>();
            dto.setT(bal);
            String picture = bal.getStr("picture");
            if (!Strings.isNullOrEmpty(picture)) {
                final List<String> pics = AppFunc.COMMA_SPLITTER.splitToList(picture);
                dto.setPics(pics);
            }
            dtoList.add(dto);
        }
        setAttr("dtoList", dtoList);
        setAttr("type", SHARE);
        render("mf_share.ftl");
    }

    /**
     * 我的活动赞
     */
    public void like() {
        int mid = getParaToInt(Const.MEMBER_PARAM, 0);
        logger.debug("like---------------mid=" + mid);
        setAttr(Const.MEMBER_PARAM, mid);

        List<ActivityDTO<BrandActivityLike>> dtoList = Lists.newArrayList();
        ActivityDTO<BrandActivityLike> dto;
        List<BrandActivityLike> balList = BrandActivityLike.dao.findByMemberId(mid);
        for (BrandActivityLike bal : balList) {
            dto = new ActivityDTO<BrandActivityLike>();
            dto.setT(bal);
            String picture = bal.getStr("picture");
            if (!Strings.isNullOrEmpty(picture)) {
                final List<String> pics = AppFunc.COMMA_SPLITTER.splitToList(picture);
                dto.setPics(pics);
            }
            dtoList.add(dto);
        }
        setAttr("dtoList", dtoList);
        setAttr("type", LIKE);
        render("mf_share.ftl");
    }
}
